package io.renren.modules.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.constant.Constant;
import io.renren.common.service.impl.BaseServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.blog.entity.BlogArticleEntity;
import io.renren.modules.blog.dao.BlogArticleDao;
import io.renren.modules.blog.dto.BlogArticleDto;
import io.renren.modules.blog.entity.BlogArticleTagEntity;
import io.renren.modules.blog.service.BlogArticleService;
import io.renren.modules.blog.service.BlogArticleTagService;
import org.springframework.stereotype.Service;
import io.renren.common.page.PageData;
import lombok.AllArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Arrays;

import org.springframework.transaction.annotation.Transactional;

/**
 * 文章表(BlogArticle)表服务实现类
 *
 * @author chy
 * @since 2024-05-23 09:56:28
 */
@AllArgsConstructor
@Service("blogArticleService")
public class BlogArticleServiceImpl extends BaseServiceImpl<BlogArticleDao, BlogArticleEntity> implements BlogArticleService {

    BlogArticleTagService blogArticleTagService;

    private IdentifierGenerator idGenerator;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BlogArticleDto get(Long id) {
        Long[] tagIds = blogArticleTagService.getTagsByArtId(id);
        BlogArticleDto blogArticleDto = ConvertUtils.sourceToTarget(baseDao.selectById(id), BlogArticleDto.class);
        blogArticleDto.setTagIds(String.valueOf(tagIds));
        return blogArticleDto;
    }

    /**
     * 分页查询
     *
     * @param params 筛选条件
     * @return 查询结果
     */
    @Override
    public PageData<BlogArticleDto> page(Map<String, Object> params) {
        paramsToLike(params, "title");
        Page<BlogArticleEntity> page = (Page<BlogArticleEntity>) getPage(params, null, false);
        // 设置排序字段
        page.addOrder(OrderItem.desc("sort"));
        page.addOrder(OrderItem.desc(Constant.UPDATE_DATE));

        List<BlogArticleDto> list = baseDao.getList(params);
        return new PageData<>(list, page.getTotal());
    }

    private QueryWrapper<BlogArticleEntity> getWrapper(Map<String, Object> params) {
        return null;
    }

    /**
     * 新增数据
     *
     * @param blogArticleDto 实例对象
     */
    @Transactional(rollbackFor = Exception.class)
    public void save(BlogArticleDto blogArticleDto) {
        BlogArticleEntity entity = ConvertUtils.sourceToTarget(blogArticleDto, BlogArticleEntity.class);
        String[] tagIds = blogArticleDto.getTagIds().split(",");
        Long articleId = entity.getId();
        if (tagIds.length > 0) {
            ArrayList<BlogArticleTagEntity> blogArticleTagEntities = new ArrayList<>();
            for (String tagId : tagIds) {
                BlogArticleTagEntity blogArticleTagEntity = new BlogArticleTagEntity();
                blogArticleTagEntity.setId((Long) idGenerator.nextId(blogArticleTagEntity));
                blogArticleTagEntity.setTagId(Long.valueOf(tagId));
                blogArticleTagEntity.setArticleId(articleId);
                blogArticleTagEntities.add(blogArticleTagEntity);
            }

            blogArticleTagService.insertArticleTags(blogArticleTagEntities);
        }
        //保存文章
        insert(entity);
    }

    /**
     * 修改数据
     *
     * @param blogArticleDto 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BlogArticleDto blogArticleDto) {
        BlogArticleEntity entity = ConvertUtils.sourceToTarget(blogArticleDto, BlogArticleEntity.class);
        String[] tagIds = blogArticleDto.getTagIds().split(",");
        Long articleId = entity.getId();
        blogArticleTagService.deleteByArtId(articleId);
        if (tagIds.length > 0) {
            ArrayList<BlogArticleTagEntity> blogArticleTagEntities = new ArrayList<>();
            for (String tagId : tagIds) {
                BlogArticleTagEntity blogArticleTagEntity = new BlogArticleTagEntity();
                blogArticleTagEntity.setId((Long) idGenerator.nextId(blogArticleTagEntity));
                blogArticleTagEntity.setTagId(Long.valueOf(tagId));
                blogArticleTagEntity.setArticleId(articleId);
                blogArticleTagEntities.add(blogArticleTagEntity);
            }

            blogArticleTagService.insertArticleTags(blogArticleTagEntities);
        }

        //更新角色
        updateById(entity);
    }

    /**
     * 通过主键删除数据
     *
     * @param ids 主键
     */

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long[] ids) {
        //删除角色
        baseDao.deleteBatchIds(Arrays.asList(ids));
    }
}
